<?php

namespace Database\Seeders;

use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class RbacSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->menuSeeder();
        $this->roleSeeder();
    }

    protected function menuSeeder()
    {
        $date = now();
        $menuData = [
            [
                'type' => 'menu',
                'title' => '控制台',
                'name' => 'dashboard',
                'icon' => 'HomeFilled',
                'path' => '/dashboard',
                'component' => 'dashboard/Dashboard',
                'sort' => null,
                'created_at' => $date,
                'children' => []
            ],[
                'type' => 'menu',
                'title' => '广告管理',
                'name' => 'banner',
                'icon' => 'PictureFilled',
                'path' => '/banner',
                'component' => 'banner/Banner',
                'sort' => null,
                'created_at' => $date,
                'children' => []
            ],[
                'type' => 'menu',
                'title' => '用户管理',
                'name' => 'user',
                'icon' => 'UserFilled',
                'path' => '/user',
                'component' => 'user/User',
                'sort' => null,
                'created_at' => $date,
                'children' => []
            ],[
                'type' => 'menu',
                'title' => '系统管理',
                'name' => 'system',
                'icon' => 'Setting',
                'path' => '/system',
                'component' => null,
                'sort' => null,
                'created_at' => $date,
                'children' => [
                    [
                        'type' => 'menu',
                        'title' => '账号管理',
                        'name' => 'system:account',
                        'icon' => null,
                        'path' => 'account',
                        'component' => 'system/account/Account',
                        'sort' => null,
                        'created_at' => $date
                    ],[
                        'type' => 'menu',
                        'title' => '角色管理',
                        'name' => 'system:role',
                        'icon' => null,
                        'path' => 'role',
                        'component' => 'system/role/Role',
                        'sort' => null,
                        'created_at' => $date
                    ],[
                        'type' => 'menu',
                        'title' => '菜单管理',
                        'name' => 'system:menu',
                        'icon' => null,
                        'path' => 'menu',
                        'component' => 'system/menu/Menu',
                        'sort' => null,
                        'created_at' => $date
                    ],[
                        'type' => 'menu',
                        'title' => '基本设置',
                        'name' => 'system:setting',
                        'icon' => null,
                        'path' => 'setting',
                        'component' => 'system/setting/Setting',
                        'sort' => null,
                        'created_at' => $date
                    ]
                ]
            ]
        ];

        $permissions = collect($menuData)
            ->flatMap(function ($item) {
                return array_merge(
                    [
                        [
                            'cn_name' => $item['title'],
                            'guard_name' => 'admin',
                            'name' => $item['name'],
                        ]
                    ],
                    // 子菜单的权限（无则返回空数组）
                    collect($item['children'] ?? [])->map(function ($child) {
                        return [
                            'cn_name' => $child['title'],
                            'guard_name' => 'admin',
                            'name' => $child['name'],
                        ];
                    })->toArray()
                );
            })
            ->toArray();
        // Reset cached roles and permissions
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
        DB::table('permissions')->insert($permissions);
        // update cache to know about the newly created permissions (required if using WithoutModelEvents in seeders)
        app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
        insertNestedData('menus',$menuData);
    }

    protected function roleSeeder()
    {
        $date = now();
        $insertData = [
            [
                'name' => '超级管理员',
                'slug' => 'admin',
                'created_at' => $date
            ],[
                'name' => '普通管理员',
                'slug' => 'common',
                'created_at' => $date
            ]
        ];
//        DB::table('roles')->insert($insertData);
    }
}
